package com.wenwan.manage.impl;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.wenwan.manage.pojo.AutoNumber;
import com.wenwan.manage.pojo.LogisticsObject;
import com.wenwan.manage.service.LogisticsService;
import com.wenwan.manage.setting.StatusSetting;

@Service
public class LogisticsServiceImpl implements LogisticsService {

	@Override
	public Map<String, Object> getAutoNumber(String courierNumber) {
		// TODO Auto-generated method stub
		// DataToObject<AutoNumber> dto = new DataToObject<AutoNumber>();
		Map<String, Object> dto = new HashMap<String, Object>();

		AutoNumber autoNumber = new AutoNumber();
		HttpPost httpPost = new HttpPost("http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=TEXT"
				.replaceFirst("TEXT", courierNumber));
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpEntity entity = builder.build();
		httpPost.setEntity(entity);
		try {
			HttpResponse response = httpClient.execute(httpPost);

			HttpEntity responseEntity = response.getEntity();
			if (responseEntity != null) {
				// 将响应内容转换为字符串
				String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
				System.out.println(result);
				autoNumber = new Gson().fromJson(result, AutoNumber.class);

				// dto.setData(autoNumber);
				dto.put("data", autoNumber);
				dto.put("msg", StatusSetting.MSG_200);
				dto.put("code", StatusSetting.CODE_200);
				return dto;

			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 执行提交
		dto.put("data", false);
		return dto;
	}

	@Override
	public Map<String, Object> getLogistics(String type, String postid) {
		// TODO Auto-generated method stub
		// DataToObject<LogisticsObject> dto = new DataToObject<LogisticsObject>();
		Map<String, Object> dto = new HashMap<String, Object>();

		LogisticsObject logisticsObject = new LogisticsObject();
		HttpPost httpPost = new HttpPost("http://www.kuaidi100.com/query?type=TYPE&postid=POSTID"
				.replaceFirst("TYPE", type).replaceFirst("POSTID", postid));
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpEntity entity = builder.build();
		httpPost.setEntity(entity);
		try {
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity responseEntity = response.getEntity();
			if (responseEntity != null) {
				// 将响应内容转换为字符串
				String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
				logisticsObject = new Gson().fromJson(result, LogisticsObject.class);
				System.out.println(result);

				// dto.setData(logisticsObject);
				dto.put("data", logisticsObject);
				dto.put("msg", StatusSetting.MSG_200);
				dto.put("code", StatusSetting.CODE_200);
				return dto;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 执行提交
		dto.put("data", false);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
